/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Principal;

import java.text.NumberFormat;
import java.text.ParseException;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

/**
 *
 * @author Tesla
 */
public class detalleDescuento extends javax.swing.JDialog {

    Float total;
    descuento descontado;
    Number number = null;

    /**
     * Creates new form detalleDescuentos
     */
    public detalleDescuento(java.awt.Frame parent, boolean modal, String tot, descuento desc) {
        super(parent, modal);
        initComponents();


        descontado = desc;
        NumberFormat fmt = NumberFormat.getInstance();

        try {
            number = fmt.parse(tot);
        } catch (ParseException ex) { }
        if (number==null)number = 0;
        jLabel5.setText(String.valueOf(number.floatValue()));
        jLabel8.setText(String.valueOf(number.floatValue()));
        total = number.floatValue();
        jComboBox1.removeAllItems();
        jComboBox1.addItem("0%");
        jComboBox1.addItem("5%");
        jComboBox1.addItem("10%");
        jComboBox1.addItem("15%");
        jComboBox1.addItem("20%");
        jComboBox1.addItem("25%");
        jComboBox1.addItem("30%");
        jRadioButton1.setSelected(true);
        jComboBox1.setEnabled(false);
        cantidad.setDocument(new detalleDescuento.LimitadorCaracteres());
        getRootPane().setDefaultButton(jButton1);
        cantidad.requestFocus();
        if (descontado.getCantidad() != 0) {
            recuperarDatos();
        }
        cantidad.selectAll();

    }

    void recuperarDatos() {
        if (descontado.getTipo().equalsIgnoreCase("Cantidad")) {
            esCantidad();
        } else {
            esPorcentaje();

        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jButton2 = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        cantidad = new javax.swing.JTextField();
        jRadioButton1 = new javax.swing.JRadioButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jRadioButton2 = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel8.setText("---");

        jButton1.setText("Aceptar");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
                jComboBox1PopupMenuWillBecomeInvisible(evt);
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
            }
        });
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel7.setText("$");

        jLabel2.setText("Descontar Por:");

        jLabel1.setText("Importe:");

        cantidad.setText("0.00");
        cantidad.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                cantidadKeyReleased(evt);
            }
            public void keyPressed(java.awt.event.KeyEvent evt) {
                cantidadKeyPressed(evt);
            }
        });

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setText("Cantidad");
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        jLabel5.setText("---");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel4.setText("$");

        jLabel3.setText("Importe - Descuento:");

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel6.setText("$");

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setText("Porcentaje");
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(26, 26, 26)
                                .addComponent(jLabel2))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel5))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(57, 57, 57)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jRadioButton1)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jRadioButton2))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(158, 158, 158)
                                        .addComponent(jLabel3))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(jLabel6)
                                        .addGap(8, 8, 8)
                                        .addComponent(cantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(30, 30, 30)
                                        .addComponent(jLabel7)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButton1)
                    .addComponent(jRadioButton2))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(cantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.dispose();
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        String tipo;
        float cantida, porcentaje;
        if(cantidad.getText().equals("")&&jRadioButton1.isSelected())this.dispose();
        else{
        switch (jComboBox1.getSelectedIndex()) {
            case 1:
                porcentaje = 5;
                break;
            case 2:
                porcentaje = 10;
                break;
            case 3:
                porcentaje = 15;
                break;
            case 4:
                porcentaje = 20;
                break;
            case 5:
                porcentaje = 25;
                break;
            case 6:
                porcentaje = 30;
                break;
            default:
                porcentaje = 0;
                break;
        }
        
        tipo = jRadioButton1.isSelected() ? "Cantidad" : "Porcentaje";
        cantida = jRadioButton1.isSelected() ? Float.parseFloat(cantidad.getText()) : porcentaje;
        descontado.setCantidad(cantida);
        descontado.setUnitario(Float.parseFloat(jLabel5.getText()) - Float.parseFloat(jLabel8.getText()));//unitario seria el numero de porcentaje
        descontado.setTipo(tipo);
        this.dispose();
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jComboBox1PopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_jComboBox1PopupMenuWillBecomeInvisible
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox1PopupMenuWillBecomeInvisible

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        float descuento;
        float porcentaje;
        switch (jComboBox1.getSelectedIndex()) {
            case 1:
                porcentaje = 5;
                break;
            case 2:
                porcentaje = 10;
                break;
            case 3:
                porcentaje = 15;
                break;
            case 4:
                porcentaje = 20;
                break;
            case 5:
                porcentaje = 25;
                break;
            case 6:
                porcentaje = 30;
                break;
            default:
                porcentaje = 0;
                break;
        }
        descuento = Float.parseFloat(jLabel5.getText()) - Float.parseFloat(jLabel5.getText()) * (porcentaje / 100);
        jLabel8.setText(String.valueOf(descuento) + '0');


        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void cantidadKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cantidadKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_cantidadKeyPressed

    void esCantidad() {
        jRadioButton1.doClick();
        jLabel8.setText(String.valueOf(Float.parseFloat(jLabel5.getText()) - descontado.getCantidad()) + '0');
        cantidad.setText(String.valueOf(descontado.getCantidad()) + '0');

    }
    private void cantidadKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cantidadKeyReleased
        if (!cantidad.getText().equals("")) {
            jLabel8.setText(String.valueOf(Float.parseFloat(jLabel5.getText()) - Float.parseFloat(cantidad.getText())) + '0');
        } else {
            jLabel8.setText(jLabel5.getText());
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_cantidadKeyReleased

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
        cantidad.setEnabled(true);
        jComboBox1.setSelectedIndex(0);
        jComboBox1.setEnabled(false);
        cantidad.setDocument(new detalleDescuento.LimitadorCaracteres());
        cantidad.requestFocus();
        // TODO add your handling code here:
    }//GEN-LAST:event_jRadioButton1ActionPerformed

    void esPorcentaje() {
        float descuento, porcentaje;
        porcentaje = descontado.getCantidad();
        jRadioButton2.doClick();
        switch ((int) (descontado.getCantidad())) {
            case 5:
                jComboBox1.setSelectedIndex(1);
                break;
            case 10:
                jComboBox1.setSelectedIndex(2);
                break;
            case 15:
                jComboBox1.setSelectedIndex(3);
                break;
            case 20:
                jComboBox1.setSelectedIndex(4);
                break;
            case 25:
                jComboBox1.setSelectedIndex(5);
                break;
            case 30:
                jComboBox1.setSelectedIndex(6);
                break;
            default:
                jComboBox1.setSelectedIndex(0);
                break;
        }
        descuento = Float.parseFloat(jLabel5.getText()) - Float.parseFloat(jLabel5.getText()) * (porcentaje / 100);
        jLabel8.setText(String.valueOf(descuento) + '0');
        cantidad.setText("0.00");
    }
    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
        jLabel8.setText(jLabel5.getText());
        cantidad.setText("");
        cantidad.setEnabled(false);
        jComboBox1.setEnabled(true);
        jComboBox1.requestFocus();
        // TODO add your handling code here:
    }//GEN-LAST:event_jRadioButton2ActionPerformed

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1MouseClicked

    /**
     * @param args the command line arguments
     */
 

    class LimitadorCaracteres extends PlainDocument {

        /**
         * Método al que llama el editor cada vez que se intenta insertar
         * caracteres. Sólo debemos verificar arg1, que es la cadena que se
         * quiere insertar en el JTextField
         */
        public void insertString(int arg0, String arg1, AttributeSet arg2) throws BadLocationException {
            for (int i = 0; i < arg1.length(); i++) // si no es digit, volvemos
            {
                if (!(Character.isDigit(arg1.charAt(i)) || arg1.charAt(i) == '.')) {
                    return;
                }
            }

            // Si todos son digit, insertamos el texto en el JTextField
            super.insertString(arg0, arg1, arg2);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JTextField cantidad;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    // End of variables declaration//GEN-END:variables
}
